from enum import Enum
from requests import get, post, put, delete

from json import JSONDecoder


class ApiResponse:
    __decoder = None

    def __init__(self, url: str, response_code: int, response_body: str):
        self.url = url
        self.response_code = response_code
        self.response_body = response_body

    def parse_body_to_json(self) -> dict:
        if not self.__decoder:
            self.__decoder = JSONDecoder()
        return self.__decoder.decode(self.response_body)


class HttpMethod(Enum):
    GET = "GET"
    POST = "POST"
    PUT = "PUT"
    DELETE = "DELETE"


class ApiRequest:
    __method_mapping = {
        HttpMethod.GET: get, HttpMethod.POST: post, HttpMethod.PUT: put,
        HttpMethod.DELETE: delete
    }

    def __init__(
            self, url: str, request_method: HttpMethod = HttpMethod.GET,
            headers: dict = None,
            form_data: dict = None,
            json_data: dict = None
    ):
        self.url = url
        self.method = request_method
        self.form_data = form_data
        self.json_data = json_data
        self.headers = headers

    def do_send(self) -> ApiResponse:
        api_request = self.__method_mapping[self.method]
        resp = api_request(url=self.url, data=self.form_data, json=self.json_data, headers=self.headers)
        return ApiResponse(resp.url, resp.status_code, resp.text)
